var app = getApp();
var RSA = require('rsa.js')
var CONFIG = require("../config/config.js");
module.exports = {
    sendImage: sendImage,
    wxRequest: wxRequest,
    userInclist:userInclist,
    provinces:provinces,
    newagainnow:newagainnow,
    newagainnow1:newagainnow1,
    uploadpic:uploadpic,
    coursecateList:coursecateList,
    courseotoType:courseotoType,
    h5wxRequest: h5wxRequest,
}

// 测试店
// var appid = 'http://81pk68921pkzone1.h5.7keya.com'

// 七颗牙正式店
var appid = 'http://171pk17641pkzone1.h5.1pkj.cn'


// 测试 
// var url1 = 'https://n.qikeya.com/qikeya_admin/public/index.php/'
function wxRequest(url, data, callback, errFun) {
    var header = {
        'appid': appid,
        'token': '',
        'version': 'v1', //版本号
        'fromsource': 5, //来源 
        'device': 'wechat',     //设备
        // 'content-type': 'application/x-www-form-urlencoded',
         'Content-type': 'application/json'
    }
    // console.log(CONFIG.API_HOST);
    wx.request({ 
        url: CONFIG.API_HOST+url,
        method: 'POST',
        data: data,
        header: header,
        dataType: 'json',
        success: function (res) {
            callback(res);
           
        },
        fail: function (err) {
            wx.hideLoading();
        }
    })
}
function h5wxRequest(url, data, callback, errFun) {
    var header = {
        'appid': appid,
        'token': '',
        'version': 'v1', //版本号
        'fromsource': 5, //来源 
        'device': 'wechat',     //设备
        'content-type': 'application/x-www-form-urlencoded'
    }
    // console.log(CONFIG.API_HOST);
    wx.request({
        url: CONFIG.API_UP + url,
        method: 'POST',
        data: data,
        header: header,
        dataType: 'json',
        success: function (res) {
            callback(res);

        },
        fail: function (err) {
            wx.hideLoading();
        }
    })
}


// 上传图片
function uploadpic(filePath,callback,filaback){
    // console.log(filePath)
    // console.log(CONFIG.API_UP)
    wx.uploadFile({
        // url: CONFIG.API_HOST + 'crm/user/insertUserCourseNew',
        url: CONFIG.API_UP + 'service/tool/upload',
        filePath: filePath,
        name: 'file',
        header: {
            'content-type': 'multipart/form-data',
            'appid': appid,
            'version': 'v1', //版本号
            'fromsource': 5, //来源 
            'device': 'wechat',     //设备
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
            // console.log(res)
            var data = JSON.parse(res.data);
            if(Number(data.code)==200){
                callback(data);
            }else{
                filaback(data)
            }
        }
    })
}


// 获取意向客户列表
function userInclist(callback){
    wxRequest('crmapi/user/userIncl',{},function(res){
        if(Number(res.data.errcode)==0){
            var data = res.data.data;
            var obj = [{
                id:0,
                name:'请选择'
            }]
            var obj1 = obj.concat(data);
            callback(obj1);
            // return obj1;
        }
    })
}

// 获取地址
function provinces(callback){
    
    wxRequest('crmapi/user/provinces',{},function(res){
        if(Number(res.data.errcode)==0){
            var data = res.data.data;
            callback(data);
            // return obj1;
        }
    })
}


// 获取课程分类
function coursecateList(callback){
    wxRequest('crmapi/User/cateList',{},function(res){
        callback(res);
    })
}

// 获取付款方式
function courseotoType(callback){
    wxRequest('crmapi/User/otoType',{},function(res){
        callback(res);
    })
}

// 获取当前时间
function newagainnow(callback,callback1,callback2){
    var that = this;
    var date1=new Date();     //1. js获取当前时间
    var timestamp1 = Date.parse(date1); 
    let date = new Date(timestamp1);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    var str = y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
    var str2 = y + '-' + MM + '-' + d + ' ' + h + ':' + m
    var str1 = y
    callback(str);
    callback1(str1);
    callback2(str2);
}

// 获取当前时间
function newagainnow1(callback){
    var that = this;
    var date1=new Date();     //1. js获取当前时间
    var min=date1.getMinutes();  //2. 获取当前分钟
    date1.setMinutes(min+60);  //3. 设置当前时间+10分钟：把当前分钟数+10后的值重新设置为date对象的分钟数
    var timestamp1 = Date.parse(date1); 
    let date = new Date(timestamp1);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    MM = MM < 10 ? ('0' + MM) : MM;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let m = date.getMinutes();
    m = m < 10 ? ('0' + m) : m;
    let s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;
    var str = y + '-' + MM + '-' + d + ' ' + h + ':' + m
    callback(str);
}



function sendImage(tempFilePath){
    
    wx.previewImage({
        current: tempFilePath, // 当前显示图片的http链接  
        urls: [tempFilePath] // 需要预览的图片http链接列表  
    })
}







